package com.xysl.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.core.utils.StringUtils;
import com.xysl.api.domain.UserCustomizeValue;
import com.xysl.constant.XyslConstants;
import com.xysl.domain.dto.QueryUserCustomizeDTO;
import com.xysl.domain.vo.GartenHomeDailyVo;
import com.xysl.domain.vo.GartenHomePageVo;
import com.xysl.domain.vo.UserCustomizeVo;
import com.xysl.mapper.KindergartenMapper;
import com.xysl.mapper.UserCustomizeValueMapper;
import com.xysl.service.IUserCustomizeOptionService;
import com.xysl.service.KindergartenService;
import io.jsonwebtoken.lang.Collections;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;


@Service
public class KindergartenServiceImpl implements KindergartenService {


    @Resource
    IUserCustomizeOptionService userCustomizeOptionService;
    @Resource
    KindergartenMapper kindergartenMapper;
    @Resource
    UserCustomizeValueMapper valueMapper;


    /**
     * 幼儿园-首页头部统计数据
     * @param id
     * @param collectCycle
     * @return
     */
    @Override
    public List<GartenHomePageVo.CollectData> collectData(Long id, String collectCycle) {
        String beginTime= "";
        String endTime= "";
        List<GartenHomePageVo.CollectData> list = null;
        switch (collectCycle) {
            case "today":
                beginTime = DateUtils.getTime();
                break;
            case "week":
                beginTime = DateUtils.getMondayOfThisWeek();
                break;
            case "month":
                beginTime = DateUtils.firstDayOfLocalMonth();
                break;
            case "lastMonth":
                beginTime = DateUtils.firstDayOfLastMonth();
                endTime = DateUtils.lastDayOfLastMonth();
                break;
            default:
                beginTime = DateUtils.getTime();
                break;
        }
        list = collectDataByCycle(id,beginTime,endTime);
        return list;
    }

    public List<GartenHomePageVo.CollectData> collectDataByCycle(Long userId, String beginTime, String endTime){
        List<GartenHomePageVo.CollectData> res = new ArrayList<>();
        LambdaQueryWrapper<UserCustomizeValue> valueWr = new LambdaQueryWrapper<>();
        valueWr.eq(UserCustomizeValue::getUserId,userId);
        valueWr.eq(UserCustomizeValue::getCustomizeType,"notify_follow_begin_day");
        List<UserCustomizeValue> valueList = valueMapper.selectList(valueWr);

        QueryUserCustomizeDTO param = new QueryUserCustomizeDTO();
        param.setUserId(userId);
        param.setCustomizeType("garden_home_statistics");
        List<UserCustomizeVo> customizeVoList = userCustomizeOptionService.selectUserCustomizeList(param);
        Integer num = XyslConstants.YEY_NUM_COLLECT;
        int sort = 1;
        String value = "";
        for (int i = 0; i < num; i++) {
            GartenHomePageVo.CollectData data = new GartenHomePageVo.CollectData();
            data.setCollectDataCode(customizeVoList.get(i).getCustomizeLabel());
            data.setCollectDataName(customizeVoList.get(i).getCustomizeValue());
            data.setSort(sort);
            switch (customizeVoList.get(i).getCustomizeLabel()) {
                //实际收入
                case "real_income":
                    value = "111";
                    break;
                //意向学员
                case "intention_stu":
                    value = "222";
                    break;
                //正式学员
                case "formal_stu":
                    value = "333";
                    break;
                //消课金额
                case "offset_amount":
                    value = "444";
                    break;
                //上课人次
                case "attend_class_num":
                    value = "555";
                    break;
                //请假人次
                case "leave_num":
                    value = "666";
                    break;
                //我的积分
                case "my_points":
                    value = "777";
                    break;
                default:
                    break;
            }
            data.setCollectDataValue(value);
            res.add(data);
            sort++;
        }
        return res;
    }


    /**
     * 幼儿园-首页 待办事项/学生生日 数据
     * @param userId
     * @return
     */
    @Override
    public GartenHomeDailyVo queryDailyAndStuBirth(Long userId) {
        GartenHomeDailyVo res = new GartenHomeDailyVo();
        //待办事项
        List<GartenHomeDailyVo.Daily> dailyList = queryDailyList(userId);
        res.setDailyList(dailyList);

        //学员生日(正式学员)
        String todayTime = LocalDate.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
        List<GartenHomeDailyVo.BirthdayStu> birthdayStuList = kindergartenMapper.queryBirthdayStuList(userId,todayTime.substring(5,10));
        res.setBirthdayStuList(birthdayStuList);
        return res;
    }

    public List<GartenHomeDailyVo.Daily> queryDailyList(Long userId){
        List<GartenHomeDailyVo.Daily> res = new ArrayList<>();
        QueryUserCustomizeDTO param = new QueryUserCustomizeDTO();
        param.setUserId(userId);
        param.setCustomizeType("garden_home_tip");
        List<UserCustomizeVo> dailyItemList = userCustomizeOptionService.selectUserCustomizeList(param);
        int defaultItemNum = XyslConstants.DAILY_DEFAULT_NUM;
        if (!Collections.isEmpty(dailyItemList)) {
            //没有配置 取默认配置展示item个数
            if (dailyItemList.get(0).getIsSelect().equals(0)) {
                for (int i = 0; i < dailyItemList.size(); i++) {
                    if (i == defaultItemNum){
                        break;
                    }
                    res.add(queryDailyDetailByItem(userId,dailyItemList.get(i)));
                }
            }
            for (UserCustomizeVo vo : dailyItemList) {
                if (vo.getIsSelect().equals("1")) {
                    res.add(queryDailyDetailByItem(userId,vo));
                }
            }
        }

        return res;
    }

    public GartenHomeDailyVo.Daily queryDailyDetailByItem(Long userId,UserCustomizeVo vo){
        int followNotifyBeginDay = XyslConstants.FOLLOW_NOTIFY_BEGIN_DAY;       //提前多少天,进入待办事项
        UserCustomizeValue param = new UserCustomizeValue();
        param.setUserId(userId);
        param.setCustomizeType("pend_follow_notify_begin_day");
        List<UserCustomizeValue> customizeVoList = valueMapper.selectUserCustomizeValueList(param);
        if (!Collections.isEmpty(customizeVoList)) {
            //取个人配置
            followNotifyBeginDay = Integer.valueOf(customizeVoList.get(0).getValue());
        }else {
            //todo 取园区配置
        }
        //查询待办事件开始日期
        String queryBeginTime = LocalDate.now().minusDays(followNotifyBeginDay).format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));

        GartenHomeDailyVo.Daily daily = new GartenHomeDailyVo.Daily();
        daily.setDailyCode(vo.getCustomizeLabel());
        daily.setDailyName(vo.getCustomizeName());
        switch (vo.getCustomizeLabel()) {
            //跟进意向学员
            case "pend_follow":
                daily.setDailyNum(kindergartenMapper.queryItemDailyNum(userId,queryBeginTime));
                break;
            //学员待分班
            case "pend_banding":
                daily.setDailyNum(2);
                break;
            //学员待分班
            case "banding_class_lesson":
                daily.setDailyNum(3);
                break;
            //请假待处理
            case "pend_ask_leave":
                daily.setDailyNum(4);
                break;
            //今日待跟进正式学员
            case "pend_today_follow_formal_stu":
                daily.setDailyNum(5);
                break;
            //今日待跟进历史学员
            case "pend_today_follow_his_stu":
                daily.setDailyNum(6);
                break;
            default:
                break;
        }
        return daily;
    }
}
